$(function () {
    initDg();
});

function initDg() {
    $('#dg').datagrid({
        width: '100%',
        height: 'auto',
        url: '/roomInfo/list',
        fitColumns: true,
        autoRowHeight: false,
        striped: true,
        method: "GET",
        idField: "fdId",
        loadMsg: "正在加载数据...",
        pagination: true,
        rownumbers: true,
        pagePosition: 'bottom',
        pageSize: 50,
        pageList: [20, 30, 40, 50],
        showFooter: true,
        fit: true,
        queryParams: {
            fd_name: $('#username').val(),
            fd_race_id:$('#raceid').combobox('getValue'),
            fd_game_id:$('#game').combobox('getValue')
        },
        columns: [[
            {field: 'fdId', title: 'ID', width: 100, align: 'center', checkbox: true},
            {
                field: 'fdName',
                title: '房间名称',
                width: 100
            },
            {
                field: 'fdNum',
                title: '房间最多人数',
                width: 100
            },
            {
                field: 'fdClanAName',
                title: '战队A',
                width: 100
            },
            {
                field: 'fdClanAWinNum',
                title: '战队A胜利场数',
                width: 100
            },
            // {
            //     field: 'fdClanAScale',
            //     title: '战队A赔率',
            //     width: 100,
            //     formatter: function (value, row, index) {
            //         return "1 : " + value
            //     }
            // },
            {
                field: 'fdClanATotalScale',
                title: '战队A总场赔率',
                width: 100,
                formatter: function (value, row, index) {
                    return "1 : " + value
                }
            },
            // {
            //     field: 'fdAKillNum',
            //     title: '战队A击杀人数',
            //     width: 100,
            //     formatter: function (value, row, index) {
            //         if (value === 0) {
            //             return "比赛未结束";
            //         } else {
            //             return value + "  人";
            //         }
            //     }
            // },
            {
                field: 'fdClanBName',
                title: '战队B',
                width: 100
            },
            {
                field: 'fdClanBWinNum',
                title: '战队B胜利场数',
                width: 100
            },
            // {
            //     field: 'fdClanBScale',
            //     title: '战队B单场赔率',
            //     width: 100,
            //     formatter: function (value, row, index) {
            //         return "1 : " + value
            //     }
            // },
            {
                field: 'fdClanBTotalScale',
                title: '战队B总场赔率',
                width: 100,
                formatter: function (value, row, index) {
                    return "1 : " + value
                }
            },
            // {
            //     field: 'fdBKillNum',
            //     title: '战队B击杀人数',
            //     width: 100,
            //     formatter: function (value, row, index) {
            //         if (value === 0) {
            //             return "比赛未结束";
            //         } else {
            //             return value + "  人";
            //         }
            //     }
            // },
            {
                field: 'fdLetClanName',
                title: '让场战队',
                width: 100
            },
            {
                field: 'fdLetNum',
                title: '让场数量',
                width: 100
            },
            {
                field: 'fdTotalKill',
                title: '总场击杀人数',
                width: 100
            },
            // {
            //     field: 'fdSizeScale',
            //     title: '大小单双赔率',
            //     width: 80,
            //     formatter: function (value, row, index) {
            //         return "1 : " + value
            //     }
            // },
            {
                field: 'fdTotalSizeScale',
                title: '总场大小单双赔率',
                width: 80,
                formatter: function (value, row, index) {
                    return "1 : " + value
                }
            },
            {
                field: 'fdSpecialScale',
                title: '特码赔率',
                width: 80,
                formatter: function (value, row, index) {
                    return "1 : " + value
                }
            },


            {
                field: 'fdResult',
                title: '比赛结果',
                width: 100,
                formatter: function (value, row, index) {
                    if (value === 0) {
                        return "比赛中";
                    } else if (value === -1) {
                        return row.fdClanAName + "<strong style='color: #CC2222'> 赢</strong>";
                    } else if (value === 1) {
                        return row.fdClanBName + "<strong style='color: #CC2222'> 赢</strong>"
                    }
                }
            },

            {
                field: 'fdRaceName',
                title: '赛事名称',
                width: 100
            },
            {
                field: 'fdGameName',
                title: '游戏名称',
                width: 100
            },
            {
                field: 'fdCurrentNum',
                title: '当前比赛场次',
                width: 100
            },
            {
                field: 'fdStartTime',
                title: '房间开盘时间',
                width: 180
            },
            {
                field: 'fdEndTime',
                title: '房间封盘时间',
                width: 180
            },
            {
                field: 'fdIsSettle',
                title: '是否结算',
                width: 100,
                formatter: function (value, row, index) {
                    if (value === 1) {
                        return "已结算";
                    } else {
                        //value == 0
                        return "未结算"
                    }
                }
            }
        ]],
        toolbar: '#tb'
    });
}

function edit() {
    var row = $('#dg').datagrid('getSelections');
    if (row != null && row.length == 1) {
        var id = row[0].fdId;
        $('#win').window({
            width: 600,
            height: 400,
            modal: true,
            collapsible: false,
            minimizable: false,
            maximizable: false,
            resizable: true,
            iconCls: "icon-add",
            href: "/roomInfo/edit/" + id,
            title: "编辑"

        });
    } else {
        $.messager.alert('提示', '请选中一行数据操作!', 'Warning');
    }

}

function add() {
    $('#win').window({
        width: 600,
        height: 400,
        modal: true,
        collapsible: false,
        minimizable: false,
        maximizable: false,
        resizable: true,
        iconCls: "icon-add",
        href: "add.jsp",
        title: "添加"

    });
}

function remove() {
    var row = $('#dg').datagrid('getSelections');
    if (row.length === 0) {
        $.messager.alert('提示', '请选中一行数据操作!', 'Warning');
    } else {
        var ids = new Array();
        $.each(row, function (i, row) {
            ids[i] = row['fdId'];
        });
        $.messager.confirm('提示', '确认删除该条记录?', function (r) {
            if (r) {
                console.log(ids);
                $.ajax({
                    type: 'POST',
                    url: '/roomInfo/remove',
                    data: {"ids": ids},
                    success: function (r) {
                        if (r.code == 0) {
                            $.messager.alert('提示', r.msg, 'info');
                            reLoad();
                            clearSelections();
                        } else {
                            $.messager.alert('提示', r.msg, 'Warning');
                        }
                    }
                });
            }
        });
    }
}

function submitForm() {
    $('#ff').form('submit', {
        onSubmit: function () {
            return $(this).form('enableValidation').form('validate');
        },
        success: function (data) {
            var r = JSON.parse(data);
            if (r.code == 0) {
                $.messager.alert("提示", r.msg, "info");
                closeWin();
                reLoad();
                clearSelections();
            } else {
                $.messager.alert("提示", r.msg, "Warning")
            }
        }
    });
}

function clearForm() {
    $('#ff').form('clear');
}

function reLoad() {
    initDg();
}

function closeWin() {
    $("#win").window("close");
}

function clearSelections() {
    $("#dg").datagrid("clearSelections");
}
